package com.customer.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.mall.domain.Cart;
import com.customer.mall.domain.vo.CartVO;
import com.customer.mall.service.ICartService;
import com.customer.mall.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiGGG
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @GetMapping("/list")
    public ResponseResult<Page<CartVO>> pageCart(String phone,
                                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "5") Integer pageSize){
        return cartService.pageCart(phone , pageNum, pageSize);
    }

    @GetMapping("/admin/{id}")
    public ResponseResult<CartVO> getCartById(@PathVariable Integer id){
        return cartService.getCartById(id);
    }

    @PostMapping("/admin/insertOrUpdate")
    public ResponseResult<String> insertOrUpdateCart(@RequestBody Cart cart){
        return cartService.insertOrUpdateCart(cart);
    }

    @PostMapping("/admin/update")
    public ResponseResult<String> updateCart(@RequestBody Cart cart){
        return cartService.updateCart(cart);
    }

    @PostMapping("/admin/delete")
    public ResponseResult<String> deleteCart(@RequestBody List<Integer> ids){
        return cartService.removeBatchByIds(ids) ? ResponseResult.okResult() : ResponseResult.failResult();
    }

}
